#include "data_manager.h"
#include "type_define.h"
#include <QDebug>
#include <qglobal.h>

DataManager::DataManager()
{
}
DataManager::~DataManager()
{
}

QVector<QByteArray> DataManager::getDatas()
{
    QVector<QByteArray> datas;
    QByteArray data = getData();
    while (!data.isEmpty()) {
        datas.append(data);
        data = getData();
    }

    return datas;
}

QByteArray DataManager::getData()
{
    if (size() < 2) {
        return QByteArray();
    }
    // 判断标识符
    int first = -1, second = -1;
    for (int i = 0; i < size(); i++) {
        if (-1 == first && at(i) == SignFlag) {
            first = i;
        } else if (at(i) == SignFlag) {
            second = i;
            break;
        }
    }

    if (-1 == second) {
        return QByteArray();
    }

    QByteArray data = mid(first, second - first + 1);
    // 移除已找到的数据
    QByteArray other = mid(second + 1);
    swap(other);

    return data;
}